package main

import (
	"crypto/sha256"
	"encoding/hex"
	"strconv"
	"time"
)

type NodeMessage struct{
	kind string
	data string
	hash string
	preHash string
	timestamp int64
	payloadHash string
}

func NewNodeMessage(data string) *NodeMessage{
	m := new(NodeMessage)
	m.data = data
	m.kind = "ID"
	m.timestamp = time.Now().UnixNano()
	m.payloadHash = m.hashPayload()
	return m
}


func (m *NodeMessage) hashPayload()(ph string) {
	message := strconv.FormatInt(m.timestamp,10) + m.data
	msgHash := sha256.Sum256([]byte(message))
	//进行十六进制字符串编码
	ph = hex.EncodeToString(msgHash[:])
	return
}

